package com.begye.smartloan.apptest.daohang

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import com.begye.smartloan.apptest.R
import com.begye.smartloan.apptest.databinding.DaoHangBottomBinding
import com.begye.smartloan.apptest.ext.addBold
import com.begye.smartloan.apptest.ext.addNormal

class DaoHangBottom : LinearLayout {

    companion object {
        const val TYPE_HOME = 0
        const val TYPE_HUAN_KUAN = 1
    }

    private var _binding: DaoHangBottomBinding? = null
    private val binding get() = _binding!!

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : this(
        context,
        attrs,
        defStyleAttr,
        0
    )

    @SuppressLint("Recycle")
    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int,
    ) : super(
        context,
        attrs,
        defStyleAttr,
        defStyleRes
    ) {
        context?.let {
            val layout = View.inflate(it, R.layout.dao_hang_bottom, this)
            _binding = DaoHangBottomBinding.bind(layout)
        }
    }

    override fun onFinishInflate() {
        super.onFinishInflate()
        // Ensure binding is not null after inflation
        if (_binding == null) {
            throw IllegalStateException("ViewBinding not initialized for ${javaClass.simpleName}")
        }
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        _binding = null  // Clear the binding reference
    }

    fun initConfig(
        type: Int = TYPE_HOME,
        onClickAction: (Int) -> Unit
    ) {
        binding.daoHangBottomHome.setOnClickListener {
            onClickAction.invoke(TYPE_HOME)
        }
        binding.daoHangBottomHuanKuan.setOnClickListener {
            onClickAction.invoke(TYPE_HUAN_KUAN)
        }

        if (type == TYPE_HOME) {
            binding.daoHangBottomHomeTriangle.visibility = View.VISIBLE
            binding.daoHangBottomHomeIcon.setImageResource(R.drawable.dres_94)
            binding.daoHangBottomHomeTitle.setTextColor(Color.parseColor("#000000"))
            binding.daoHangBottomHomeTitle.addBold()

            binding.daoHangBottomHuanKuanTriangle.visibility = View.GONE
            binding.daoHangBottomHuanKuanIcon.setImageResource(R.drawable.dres_95)
            binding.daoHangBottomHuanKuanTitle.setTextColor(Color.parseColor("#ADB4CC"))
            binding.daoHangBottomHuanKuanTitle.addNormal()

        } else {
            binding.daoHangBottomHomeTriangle.visibility = View.GONE
            binding.daoHangBottomHomeIcon.setImageResource(R.drawable.dres_104)
            binding.daoHangBottomHomeTitle.setTextColor(Color.parseColor("#ADB4CC"))
            binding.daoHangBottomHomeTitle.addNormal()

            binding.daoHangBottomHuanKuanTriangle.visibility = View.VISIBLE
            binding.daoHangBottomHuanKuanIcon.setImageResource(R.drawable.dres_105)
            binding.daoHangBottomHuanKuanTitle.setTextColor(Color.parseColor("#000000"))
            binding.daoHangBottomHuanKuanTitle.addBold()
        }
    }

}